package com.roc.admin.interfaces.controller;

import com.roc.admin.application.service.RoleService;
import com.roc.admin.interfaces.common.Result;
import com.roc.admin.interfaces.dto.RoleDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色控制器
 */
@Tag(name = "角色管理", description = "角色管理相关接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @Operation(summary = "创建角色")
    @PostMapping
    public Result<Long> createRole(@Validated @RequestBody RoleDTO roleDTO) {
        Long roleId = roleService.createRole(roleDTO);
        return Result.success(roleId);
    }

    @Operation(summary = "更新角色")
    @PutMapping
    public Result<Boolean> updateRole(@Validated @RequestBody RoleDTO roleDTO) {
        boolean success = roleService.updateRole(roleDTO);
        return Result.success(success);
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{roleId}")
    public Result<Boolean> deleteRole(@Parameter(description = "角色ID") @PathVariable Long roleId) {
        boolean success = roleService.deleteRole(roleId);
        return Result.success(success);
    }

    @Operation(summary = "获取角色信息")
    @GetMapping("/{roleId}")
    public Result<RoleDTO> getRoleById(@Parameter(description = "角色ID") @PathVariable Long roleId) {
        RoleDTO roleDTO = roleService.getRoleById(roleId);
        return Result.success(roleDTO);
    }

    @Operation(summary = "获取角色列表")
    @GetMapping("/list")
    public Result<List<RoleDTO>> getRoleList() {
        List<RoleDTO> roleDTOList = roleService.getRoleList();
        return Result.success(roleDTOList);
    }

    @Operation(summary = "根据用户ID获取角色列表")
    @GetMapping("/list/user/{userId}")
    public Result<List<RoleDTO>> getRoleListByUserId(@Parameter(description = "用户ID") @PathVariable Long userId) {
        List<RoleDTO> roleDTOList = roleService.getRoleListByUserId(userId);
        return Result.success(roleDTOList);
    }

    @Operation(summary = "分配角色菜单")
    @PostMapping("/menu")
    public Result<Boolean> assignMenus(@Parameter(description = "角色ID") @RequestParam Long roleId,
                                      @Parameter(description = "菜单ID列表") @RequestParam List<Long> menuIds) {
        boolean success = roleService.assignMenus(roleId, menuIds);
        return Result.success(success);
    }

    @Operation(summary = "分配角色部门")
    @PostMapping("/dept")
    public Result<Boolean> assignDepts(@Parameter(description = "角色ID") @RequestParam Long roleId,
                                      @Parameter(description = "部门ID列表") @RequestParam List<Long> deptIds) {
        boolean success = roleService.assignDepts(roleId, deptIds);
        return Result.success(success);
    }
}
